//
//  ListViewController.m
//  RACDemo
//
//  Created by XFB on 2018/8/6.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "ListViewController.h"
#import "ReactiveObjC.h"
#import "RACReturnSignal.h"

@interface ListViewController ()

- (IBAction)backButtonDidClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.textField.text = self.string;

    /// 信号中信号: 信号发送一个信号
//    [self test1];

    [self test2];
}

- (void)test2
{
    /// fattenMap: 信号中信号
//    [[_textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) {
//        NSString *result = [NSString stringWithFormat:@"ZC %@", value];
//        return [RACReturnSignal return:result];
//    }] subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@", x);
//    }];

    [[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
        NSString *result = [NSString stringWithFormat:@"ZC %@", value];
        return result;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];
}

- (void)test1
{
    RACSubject *signalOfSignals = [RACSubject subject];
    RACSubject *signal = [RACSubject subject];
    
    [signalOfSignals subscribeNext:^(id  _Nullable x) {
        NSLog(@"信号中信号的值:%@", x);
        [x subscribeNext:^(id  _Nullable x) {
            NSLog(@"信号的值:%@", x);
        }];
    }];
    
    /// falttenMap, map获取都是信号的值
    [[signalOfSignals flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        
        __block NSString *result;
        
//        [value subscribeNext:^(id  _Nullable x) {
//            NSLog(@"%@", x);
//            result = [NSString stringWithFormat:@"%@", x];
//        }];
        
        return [value map:^id _Nullable(id  _Nullable value) {
            NSLog(@"%@", value);
            return [NSString stringWithFormat:@"%@", value];
        }];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

    [signalOfSignals sendNext:signal];
    [signal sendNext:@"999"];
}

- (IBAction)backButtonDidClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];

    [self.delegate passValue:self.textField.text];
}

@end
